'''
Created on Nov 23, 2010

@author: namikata
'''

import math
import gameobjects
from gameobjects.vector2 import Vector2
import pyglet
from game.effect.EffectLibrary import EffectLibrary

KMOV_DIRECTION, KMOV_DESTINATION, KMOV_NONE = (0,1,2)
KCLASS_PLAYER, KCLASS_CURSOR, KCLASS_SHOOT, KCLASS_EFFECT, KCLASS_BLOCK, KCLASS_UI = (0,1,2,3,4,5)
KSCLASS_PLAYERHUMAN, KSCLASS_PLAYERHUMANFRIEND, KSCLASS_PLAYERHUMANENEMY, KSCLASS_PLAYERCPUFRIEND, KSCLASS_PLAYERCPUENEMY = (0,1,2,3,4)
KSCLASS_CURSORPLAYER, KSCLASS_CURSORFRIEND, KSCLASS_CURSORENEMY, KSCLASS_CURSORWEAPONFRIEND, KSCLASS_CURSORWEAPONENEMY = (0,1,2,3,4)
KSCLASS_SHOOTBULLET, KSCLASS_SHOOTLASER, KSCLASS_SHOOTBOMB = (0,1,2)
KSCLASS_UIMENU, KSCLASS_UIMESSAGE, KSCLASS_UIPLAYER = (0,1,2)

def get_rectangle(x, y, w, h, c):
    """position_x, position_y, width, height, color RGB"""
    verts1 = (x,y,
             x,y+h,
             x+w,y+h,)
    verts2 = (x+w,y+h,
             x+w,y,
             x,y,)
    colors1 = (c[0],c[1],c[2],
              c[0], c[1], c[2], 
              c[0], c[1], c[2],)
    colors2 = (c[0],c[1],c[2],
              c[0], c[1], c[2], 
              c[0], c[1], c[2],)
    return {"v1":verts1, "c1":colors1, "v2":verts2, "c2":colors2}

def get_angle(point_a, point_b):
    #point_a.normalize()
    #point_b.normalize()
    heading = Vector2(*point_b) - Vector2(*point_a)
    heading.normalize()
    angle = (math.atan2(heading.y,heading.x) * 180 / math.pi) - 90
    #if angle < 0:
    #    angle = float(360 - angle)
    if angle >= 360:
        angle = angle - 360
    return -angle

def rotate_at_point(self, origin, angle):
    #sin and cos are slow, change to lookup tables
    self = Vector2(*self)
    origin = Vector2(origin)
    sinT = math.sin(math.radians(angle))
    cosT = math.cos(math.radians(angle))
    return Vector2(origin.x + (cosT * (self.x - origin.x) - sinT * (self.y - origin.y)),
                      origin.y + (sinT * (self.x - origin.x) + cosT * (self.y - origin.y)))

class GameEntity(object):

    world = None
    name = None
    sprite = None
    direction = None
    destination = None
    movement_type = None
    speed = None
    max_speed = None
    accel = None
    brain = None
    id = None
    objclass = None
    objsclass = None
    destroy = False
    scaled_image = None
    effect_library = None

    def __init__(self, world, name, image = None, objclass = None, objsclass = None):
        self.world = world
        self.name = name
        self.sprite = pyglet.sprite.Sprite(image, 0,0)
        self.direction = Vector2(0,0)
        self.destination = Vector2(0,0)
        self.movement_type = KMOV_DIRECTION
        self.speed = 0.
        self.max_speed = 0.
        self.accel = 0.
        #self.brain = BrainMachine()
        self.id = 0
        self.objclass = objclass
        self.objsclass = objsclass
        self.destroy = False
        self.effect_library = EffectLibrary(self)

    def render(self):
        self.effect_library.render()

    def update(self, time_elapsed):
        
        self.effect_library.update(float(time_elapsed))
        
        #self.brain.think()
        if abs(self.speed) > 0 and self.movement_type != KMOV_NONE:
            travel_distance = 0
            heading = (0,0)
            if self.movement_type == KMOV_DESTINATION:
                vec_to_destination = self.destination - self.location
                distance_to_destination = vec_to_destination.get_length()
                heading = vec_to_destination.get_normalized()
                travel_distance = min(distance_to_destination, time_elapsed * self.speed)
            if self.movement_type == KMOV_DIRECTION and self.direction != (0,0):
                heading = self.direction.get_normalized()
                if heading != (0,0):
                    travel_distance = time_elapsed * self.speed
            
            displacement = Vector2(*(travel_distance * heading))
            position = Vector2(*self.sprite.position) + displacement
            self.sprite.set_position(position.x, position.y)